#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<math.h>
using namespace std;

class Solution {
public:
    bool isHappy(int n) {
        set<int> S;
        return func(n,S);

    }
    bool func(int n,set<int>& S){
        if(S.insert(n).second==false){
            return false;
        }
        int sum = 0;
        while(n){
            if(n%10!=0)
                sum += pow(n%10,2);
            n = n/10;
        }
        if(sum==1){
            return true;
        }
        return func(sum,S);
    }    
};


int main(){
    Solution S;
    cout<<S.isHappy(5)<<endl;
}

